package com.tmzn.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.tmzn.domain.vo.TDeviceRecordVO;
import com.tmzn.entity.param.DeviceParam;
import com.tmzn.mapper.TDeviceRecordMapper;
import com.tmzn.service.ITDeviceRecordService;
import com.tmzn.util.SplitDataUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class TDeviceRecordServiceImpl implements ITDeviceRecordService {
    @Autowired
    TDeviceRecordMapper tDeviceRecordMapper;

    @Override
    public TDeviceRecordVO getStandardByDeviceId(DeviceParam deviceParam) {
        return null;
    }

    @Override
    public JSONObject getPortDetail(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String time = deviceParam.getTime();
        int splitTableNum = SplitDataUtil.getSplitTableNum(deviceId + "");
        Map<String, Object> deviceData = tDeviceRecordMapper.getDeviceData("select * from t_device_record_" + splitTableNum + " where device_id = '" + deviceId + "' and type = 103 and create_time>='" + time + "' order by create_time desc limit 1");
        if(deviceData != null){
            Object detail = deviceData.get("detail");
            JSONObject jsonObject = JSONObject.parseObject(detail.toString());
            JSONObject real_data = jsonObject.getJSONObject("real_data");
            return real_data;
        }
        return null;
    }




    @Override
    public JSONObject getMainboardConfig(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String time = deviceParam.getTime();
        int splitTableNum = SplitDataUtil.getSplitTableNum(deviceId + "");
        Map<String, Object> deviceData = tDeviceRecordMapper.getDeviceData("select * from t_device_record_" + splitTableNum + " where device_id = '" + deviceId + "' and type = 96 and create_time>='" + time + "' order by create_time desc limit 1");
        if(deviceData != null){
            Object detail = deviceData.get("detail");
            JSONObject jsonObject = JSONObject.parseObject(detail.toString());
            JSONObject real_data = jsonObject.getJSONObject("real_data");
            return real_data;
        }
        return null;
    }

    @Override
    public JSONObject checkStatus(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String time = deviceParam.getTime();
        int splitTableNum = SplitDataUtil.getSplitTableNum(deviceId + "");
        Map<String, Object> deviceData = tDeviceRecordMapper.getDeviceData("select * from t_device_record_" + splitTableNum + " where device_id = '" + deviceId + "' and type in(116,113) and create_time>='" + time + "' order by create_time desc limit 1");
        if(deviceData != null){
            JSONObject object = new JSONObject();
            Integer type = (Integer) deviceData.get("type");
            object.put("type",type);
            return object;
        }
        return null;
    }

}
